/*
    Heart of Gold engine management system
    Copyright (C) 2004 Heart of Gold development team

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.heartOfGold.model.megasquirtECU;

import java.util.Iterator;

import com.heartOfGold.model.Formatter;
import com.heartOfGold.model.Message;

/*
 * @author Ryan Higgins
 * @date 2004
 *
 * Module to translate Strings <-> Megasquirt Message Objects.
 */


public final class MegaFormatter extends Formatter {
    private static MegaFormatter instance;
    // private char lastMessage = '0';
    private int byteCount = 0;
    private int waitFor = 0;

    private byte[] buffer = new byte[125];

    private MegaFormatter() {

    }

    public static MegaFormatter getMega() {
        if (instance == null) {
            instance = new MegaFormatter();
        }
        return instance;
    }

    public Message parseBytes(final byte[] bytes, final int numBytes) {
        if (numBytes == 0) {
            System.out.println("Error: parseBytes() "
                    + "called on an empty byte array");
            return null;
        }
        for (int i = 0; i < numBytes; i++) {
            buffer[byteCount + i] = bytes[i]; // TODO check this!!!
        }
        byteCount += numBytes;
        if (byteCount == waitFor) {
            Message messageObject = new Message();
            messageObject.setID("fuel");
            messageObject.addArg(buffer);
            messageObject.addArg(new Integer(byteCount));
            return messageObject;
        }
        return null;
    }

    public byte[] parseMessage(final Message message) {
        int numBytes = 0;
        byte[] temp = null;
        byte[] parsed = null;
        int i = 0;
        if (message == null) {
            System.out.println("Error: parseMessage()"
                    + " called on a null message");
            return null;
        }
        Iterator<Object> args = message.getArgs();
        if (args.hasNext()) {
            temp = (byte[]) args.next();
        }
        if (args.hasNext()) {
            numBytes = ((Integer) args.next()).intValue();
        }
        parsed = new byte[numBytes];
        for (i = 0; i < numBytes; i++) {
            parsed[i] = temp[i];
        }
        if ((char) parsed[0] == 'V') {
            byteCount = 0;
            waitFor = 125;
        }
        if ((char) parsed[0] == 'A') {
            // if we're still waiting for a response
            // don't send another request
            if ((waitFor == 22) && (byteCount < 22)) {
                return null;
            }
            byteCount = 0;
            waitFor = 22;
        }
        return parsed;

    }
}
